package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.event.CoverPicEvent;
import com.cljz.makepassenger.model.PublishReqBean;
import com.cljz.makepassenger.model.map.City;
import com.cljz.makepassenger.model.net.bean.GetTypeListBean;
import com.cljz.makepassenger.model.net.bean.InviteContentBean;
import com.cljz.makepassenger.model.net.bean.InviteDetailBean;
import com.cljz.makepassenger.present.activity.EditorInvitedP;
import com.cljz.makepassenger.ui.ItemDecoration.GridSpacesItemDecoration;
import com.cljz.makepassenger.ui.ItemDecoration.SpacesItemDecoration;
import com.cljz.makepassenger.ui.adapter.ModelAddAdapter;
import com.cljz.makepassenger.ui.adapter.PicAdapter;
import com.cljz.makepassenger.utils.L;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;
import com.cljz.makepassenger.utils.date.TextUtil;
import com.cljz.makepassenger.widget.FlagButton;
import com.cljz.makepassenger.widget.SelectTime;
import com.cljz.makepassenger.widget.datetimpick.TimeSelector;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/7.
 * 发起邀请页面
 */

public class EditorInvitedActivity extends BaseActivity<EditorInvitedP> implements CompoundButton.OnCheckedChangeListener, ModelAddAdapter.OnItemClickListener, PicAdapter.OnItemClickListener {

    public static final int EDITOR_INVITE = 0;
    public static final int REVISE_INVITE = 1;
    public static final int EDITOR_ACTIVITY = 2;

    @BindView(R.id.tv_point_tip)
    TextView tvPointTip;
    @BindView(R.id.sv_main)
    ScrollView svMain;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_title_time)
    TextView tvTitleTime;
    @BindView(R.id.tv_title_date)
    TextView tvTitleDate;
    @BindView(R.id.ll_title_time)
    LinearLayout llTitleTime;
    @BindView(R.id.rl_time_title)
    RelativeLayout rlTimeTitle;
    @BindView(R.id.st_time_select)
    SelectTime stTimeSelect;
    @BindView(R.id.tv_local_select)
    TextView tvLocalSelect;
    @BindView(R.id.tv_local_details)
    TextView tvLocalDetails;
    @BindView(R.id.ll_local_select)
    LinearLayout llLocalSelect;
    @BindView(R.id.iv_pen)
    ImageView ivPen;
    @BindView(R.id.et_invite)
    EditText etInvite;
    @BindView(R.id.tv_word_number)
    TextView tvWordNumber;
    @BindView(R.id.fb_things)
    FlagButton fbThings;
    @BindView(R.id.tv_nearby_open)
    TextView tvNearbyOpen;
    @BindView(R.id.switch_nearby)
    Switch switchNearby;
    @BindView(R.id.tv_name_child)
    TextView tvNameChild;
    @BindView(R.id.et_activity_title)
    EditText etActivityTitle;
    @BindView(R.id.tv_activity_title_number)
    TextView tvActivityTitleNumber;
    @BindView(R.id.rl_activity_mold)
    RelativeLayout rlActivityMold;
    @BindView(R.id.tv_activity_model)
    TextView tvActivityModel;
    @BindView(R.id.iv_activity_model_add)
    ImageButton ivActivityModelAdd;
    @BindView(R.id.tv_activity_person_num)
    TextView tvActivityPersonNum;
    @BindView(R.id.et_activity_person_num)
    EditText etActivityPersonNum;
    @BindView(R.id.tv_activity_apply_time)
    TextView tvActivityApplyTime;
    @BindView(R.id.tv_activity_start_time)
    TextView tvActivityStartTime;
    @BindView(R.id.rl_activity_start_time)
    RelativeLayout rlActivityStartTime;
    @BindView(R.id.tv_activity_end_time)
    TextView tvActivityEndTime;
    @BindView(R.id.rl_activity_end_time)
    RelativeLayout rlActivityEndTime;
    @BindView(R.id.tv_activity_detail)
    TextView tvActivityDetail;
    @BindView(R.id.tv_activity_detail_number)
    TextView tvActivityDetailNumber;
    @BindView(R.id.iv_activity_cover)
    ImageView ivActivityCover;
    @BindView(R.id.rv_activity_pic)
    RecyclerView rvActivityPic;
    @BindView(R.id.iv_invite_pic)
    ImageView ivInvitePic;
    @BindView(R.id.ll_nearby)
    LinearLayout llNearby;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.activity_invite)
    LinearLayout activityInvite;
    private List<String> selectButton = new ArrayList<>();
    //    private String selectTime;
    private InviteDetailBean.DataBean dataBean = null;
    //    private InviteInfo inviteInfo = new InviteInfo();
    private List<InviteContentBean.DataBean> beanList = new ArrayList<>();
    private City city;
    private int type;
    private String inviteTime;
    private PopupWindow modelAddWindow;

    //是否已经获取到type
    private boolean typeHasGet;
    private List<GetTypeListBean.DataBean> typeList;
    private ModelAddAdapter modelAddAdapter;
    private ArrayList<String> picList;
    private PicAdapter picAdapter;

    private PublishReqBean publishReqBean;
    private String coverPicPath;
    private String changeTime;
    private String invite_id;


    private enum WhichTime {
        INVITE_TIME, START_TIME, END_TIME
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("活动邀请");
        ivTitleRight.setImageResource(R.mipmap.icon_help_center);
        selectButton.clear();
        publishReqBean = new PublishReqBean();
        tvWordNumber.setText(Html.fromHtml("<font color='" + getResources().getColor(R.color.text_color) + "'>" + 0 + "</font>/50"), TextView.BufferType.SPANNABLE);
        getP().getInviteContent();
        this.type = getIntent().getExtras().getInt("type", -1);
        if (this.type == EDITOR_INVITE) {
            tvTitleTime.setText(GetSystemTimeUtil.getTime());
            tvTitleDate.setText(GetSystemTimeUtil.getWeekDate());
            llNearby.setVisibility(View.GONE);
            switchNearby.setChecked(false);

        } else if (type == EDITOR_ACTIVITY) {
            tvTitleTime.setText(GetSystemTimeUtil.getTime());
            tvTitleDate.setText(GetSystemTimeUtil.getWeekDate());
            switchNearby.setChecked(true);
            llNearby.setVisibility(View.VISIBLE);
            if (!typeHasGet) {
                getP().getTypeList();
            }
        } else if (type == REVISE_INVITE) {
            dataBean = (InviteDetailBean.DataBean) getIntent().getExtras().getSerializable("data");
            invite_id = getIntent().getExtras().getString("invite_id");
            tvTitleDate.setText(dataBean.getInvite().getDate() + " " + dataBean.getInvite().getWeek());
            tvTitleTime.setText(dataBean.getInvite().getTime());
            tvLocalSelect.setText(dataBean.getInvite().getLandmark());
            tvLocalDetails.setText(dataBean.getInvite().getAddress());
            etInvite.setText(dataBean.getInvite().getTitle());
            btnSubmit.setText("确认并修改");
        }
        stTimeSelect.initdata(new SelectTime.TimeDataCallBack() {
            @Override
            public void getSelectTime(String time) {
                changeTime = time;
                if (time != null) {
                    try {
                        inviteTime = time;
                        publishReqBean.setInvite_time(String.valueOf(GetSystemTimeUtil.dateToStamp(time)));
                        tvTitleTime.setText(GetSystemTimeUtil.getNowTurnTime(time));
                        tvTitleDate.setText(GetSystemTimeUtil.getTurnWeekDate(time));
                        publishReqBean.setTime_start(String.valueOf(System.currentTimeMillis()));
                        publishReqBean.setTime_end(String.valueOf(GetSystemTimeUtil.dateToStamp(time)));
                        tvActivityStartTime.setText("即日");
                        tvActivityEndTime.setText("活动开始前");
                        stTimeSelect.setButtonName(time);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else {
                    tvTitleTime.setText(GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailgetTime()));
                    tvTitleDate.setText(GetSystemTimeUtil.getTurnWeekDate(GetSystemTimeUtil.getInDetailgetTime()));
                }
            }
        }, null);
        picList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvActivityPic.setLayoutManager(linearLayoutManager);
        picAdapter = new PicAdapter(this, picList);
        rvActivityPic.setAdapter(picAdapter);
        rvActivityPic.addItemDecoration(new SpacesItemDecoration(30));

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_initiate_invite;
    }

    @Override
    public EditorInvitedP newP() {
        return new EditorInvitedP();
    }

    public void inviteContentFail(String error) {

    }

    public void inviteContentSucceed(final List<InviteContentBean.DataBean> data) {
        int size = data.size();
        for (int i = 0; i < size; i++) {
            selectButton.add(data.get(i).getInvite_content());
        }
        int[] margin = new int[]{0, 20, 0, 30};
        fbThings.initChildViews(selectButton, R.layout.item_things_list, margin, new FlagButton.OnFlagItemClick() {
            @Override
            public void OnItemClick(int position) {
                beanList.add(data.get(position));
                String selectThing = data.get(position).getInvite_content();
                String etThings = etInvite.getText().toString();
                StringBuilder stringBuilder = new StringBuilder(etThings);
//                if (!etThings.trim().contains(selectThing)) {
                stringBuilder.append(selectThing).append("  ");
                etInvite.setText(stringBuilder.toString());
                publishReqBean.setInviteContent(beanList);
//                } else {
//                    showToast("已添加该活动", R.mipmap.jinshi);
//                }
            }
        });

    }

    @Override
    protected void initListener() {
        super.initListener();
        switchNearby.setOnCheckedChangeListener(this);
        RxView.clicks(ivTitleRight)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent(EditorInvitedActivity.this).to(HelpCenterActivity.class).launch();
                    }
                });
        RxView.clicks(llLocalSelect)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).requestCode(12).to(MapActivity.class).launch();
                    }
                });
        RxTextView.textChanges(etInvite)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        tvWordNumber.setText(Html.fromHtml("<font color='" + getResources().getColor(R.color.text_color) +
                                "'>" + charSequence.length() +
                                "</font>/50"), TextView.BufferType.SPANNABLE);
//                        etInvite.setSelection(charSequence.length());
                    }
                });
        RxView.clicks(rlTimeTitle)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showSelectTime(WhichTime.INVITE_TIME);
                    }
                });
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop(EditorInvitedActivity.this);
                    }
                });
        //活动标题监听
        RxTextView.textChanges(etActivityTitle)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        tvActivityTitleNumber.setText(Html.fromHtml("<font color='" + getResources().getColor(R.color.text_color) +
                                "'>" + charSequence.length() +
                                "</font>/20"), TextView.BufferType.SPANNABLE);
                        etActivityTitle.setSelection(charSequence.length());
                    }
                });
        //报名时间点击事件
        RxView.clicks(rlActivityStartTime)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showSelectTime(WhichTime.START_TIME);
                    }
                });
        //报名时间点击事件
        RxView.clicks(rlActivityEndTime)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showSelectTime(WhichTime.END_TIME);
                    }
                });
        //活动详情点击事件
        RxView.clicks(tvActivityDetail)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .requestCode(23)
                                .putString("hasWrite", tvActivityDetail.getText().toString().trim())
                                .to(EditInviteDetailActivity.class)
                                .launch();
                    }
                });
        //活动类型点击事件
        RxView.clicks(rlActivityMold)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showModelAddWindow();
                    }
                });
        //活动封面点击事件
        RxView.clicks(ivActivityCover)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .putBoolean(UserLogoActivity.EXTRA_SHOW_CAMERA, true)
                                .putInt(UserLogoActivity.EXTRA_SELECT_COUNT, 1)
                                .putInt(UserLogoActivity.EXTRA_SELECT_MODE, UserLogoActivity.MODE_SINGLE)
                                .putIntArray(CutViewActivity.CUT_SCALE, new int[]{5, 3})
                                .to(UserLogoActivity.class)
                                .launch();
                    }
                });
        RxView.clicks(ivInvitePic)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {

                    }
                });
        picAdapter.setOnItemClickListener(this);
        RxView.clicks(btnSubmit)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (type == EDITOR_INVITE || type == EDITOR_ACTIVITY) {
                            if (city != null) {
                                publishReqBean.setRegion_id3(city.getCity_id());
                                publishReqBean.setAddress(city.getAddress());
                                publishReqBean.setLandmark(city.getAreaName());
                                publishReqBean.setLatitude(String.valueOf(city.getLatitude()));
                                publishReqBean.setLongitude(String.valueOf(city.getLongitude()));
                                if (switchNearby.isChecked()) {
                                    if ("".equals(publishReqBean.getInvite_time())) {
                                        showToast("还没选择活动时间", R.mipmap.jinshi);
                                    } else if (etInvite.getText().toString().equals("")) {
                                        showToast("去干嘛？", R.mipmap.jinshi);
                                    } else if ("".equals(tvActivityModel.getText().toString().trim())) {
                                        showToast("还没添加活动类型", R.mipmap.jinshi);
                                    } else if ("".equals(etActivityPersonNum.getText().toString().trim())) {
                                        showToast("限制一下活动人数吧", R.mipmap.jinshi);
                                    } else if (publishReqBean.getCoverBitmapPath() == null) {
                                        showToast("还没添加活动封面", R.mipmap.jinshi);
                                    } else if ("".equals(etActivityTitle.getText().toString().trim())) {
                                        showToast("给活动起个名字吧", R.mipmap.jinshi);
                                    } else if ("".equals(tvActivityDetail.getText().toString().trim())) {
                                        showToast("描述下活动吧", R.mipmap.jinshi);
                                    } else {
                                        publishReqBean.setIs_activity("1");
                                        publishReqBean.setTitle(etInvite.getText().toString().trim());
                                        publishReqBean.setActivityType(tvActivityModel.getText().toString().trim());
                                        publishReqBean.setNumber_all(etActivityPersonNum.getText().toString().trim());
                                        publishReqBean.setTitle2(etActivityTitle.getText().toString().trim());
                                        publishReqBean.setContent(tvActivityDetail.getText().toString().trim());
                                        publishReqBean.setPicList(picList);
                                        L.e(publishReqBean.toString());
                                        Router.newIntent((Activity) App.getInstance())
                                                .data(new Bundle())
                                                .putParcelable("data", publishReqBean)
                                                .putInt("type", InvitationLetterActivity.EDITOR)
                                                .to(InvitationLetterActivity.class).launch();
                                    }
                                } else {
                                    if ("".equals(publishReqBean.getInvite_time())) {
                                        showToast("请选择活动时间", R.mipmap.jinshi);

                                    } else if (etInvite.getText().toString().equals("")) {
                                        showToast("请选择活动内容", R.mipmap.jinshi);

                                    } else {
                                        publishReqBean.setTitle(etInvite.getText().toString());
                                        publishReqBean.setIs_activity("0");
                                        Router.newIntent((Activity) App.getInstance())
                                                .data(new Bundle())
                                                .putParcelable("data", publishReqBean)
                                                .putInt("type", InvitationLetterActivity.EDITOR)
                                                .to(InvitationLetterActivity.class).launch();
                                    }
                                }
                            } else {
                                showToast("去哪儿？", R.mipmap.jinshi);
                            }
                        } else if (type == REVISE_INVITE) {
                            if (city != null) {
                                publishReqBean.setRegion_id3(city.getCity_id());
                                publishReqBean.setAddress(city.getAddress());
                                publishReqBean.setLandmark(city.getAreaName());
                                publishReqBean.setLatitude(String.valueOf(city.getLatitude()));
                                publishReqBean.setLongitude(String.valueOf(city.getLongitude()));
                            }
                            if (changeTime == null) {
                                publishReqBean.setInvite_time("");
                            }
                            publishReqBean.setTitle(etInvite.getText().toString());
                            publishReqBean.setIs_activity("0");
                            getP().inviteUpdate(publishReqBean, invite_id);
                        }


//                        if (switchNearby.isChecked()) {
//
//                        } else {
//                            if (type == 0) {
//                                if (city != null) {
//                                    publishReqBean.setRegion_id3(city.getCity_id());
//                                    publishReqBean.setAddress(city.getAddress());
//                                    publishReqBean.setLandmark(city.getAreaName());
//                                    publishReqBean.setLatitude(String.valueOf(city.getLatitude()));
//                                    publishReqBean.setLongitude(String.valueOf(city.getLongitude()));
//
//                                    if ("".equals(publishReqBean.getInvite_time())) {
//                                        showSnackBar(rlTitle, "请选择活动时间");
//                                    } else if (etInvite.getText().toString().equals("")) {
//                                        showSnackBar(rlTitle, "请选择活动内容");
//                                    } else {
//                                        publishReqBean.setTitle(etInvite.getText().toString());
//                                        publishReqBean.setIs_activity("0");
//                                        Router.newIntent((Activity) App.getInstance())
//                                                .data(new Bundle())
//                                                .putSerializable("data", publishReqBean)
//                                                .putInt("type", 0)
//                                                .to(InvitationLetterActivity.class).launch();
//                                    }
//                                } else {
//                                    showSnackBar(rlTitle, "请选择活动地址");
//                                }
//                            } else if (type == 1) {
//                                String cityId = null;
//                                if (city != null) {
//                                    publishReqBean.setRegion_id3(city.getCity_id());
//                                    publishReqBean.setAddress(city.getAddress());
//                                    publishReqBean.setLandmark(city.getAreaName());
//                                    publishReqBean.setLatitude(String.valueOf(city.getLatitude()));
//                                    publishReqBean.setLongitude(String.valueOf(city.getLongitude()));
//                                }
//                                publishReqBean.setTitle(etInvite.getText().toString());
////                                publishReqBean.setInvite_time(selectTime);
//                                publishReqBean.setAddress(tvLocalDetails.getText().toString());
//                                publishReqBean.setLandmark(tvLocalSelect.getText().toString());
//                                StringBuilder stringBuilder = new StringBuilder();
//                                int size = beanList.size();
//                                for (int i = 0; i < size; i++) {
//                                    stringBuilder.append(beanList.get(i).getId()).append(",");
//                                }
//                                getP().getInviteUpdate(cityId, stringBuilder.toString(), dataBean);
//                            }
//                        }
                    }
                });
    }

    private void showModelAddWindow() {
        View modelAddView = View.inflate(this, R.layout.pop_model_add, null);
        RecyclerView rvModel = (RecyclerView) modelAddView.findViewById(R.id.rv_model);
        Button btnOk = (Button) modelAddView.findViewById(R.id.btn_ok);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        rvModel.setLayoutManager(gridLayoutManager);
        modelAddAdapter = new ModelAddAdapter(typeList, this);
        rvModel.addItemDecoration(new GridSpacesItemDecoration(20));
        rvModel.setAdapter(modelAddAdapter);
        modelAddWindow = new PopupWindow(modelAddView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        modelAddWindow.setTouchable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        modelAddWindow.setBackgroundDrawable(dw);
        modelAddWindow.showAtLocation(activityInvite, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
        RxView.clicks(btnOk)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String typeId = null;
                        String typeName = null;
                        List<GetTypeListBean.DataBean> dataBeen = modelAddAdapter.getTypeList();
                        for (int i = 0; i < dataBeen.size(); i++) {
                            GetTypeListBean.DataBean data = dataBeen.get(i);
                            if (data.getSelect()) {
                                typeId = data.getId();
                                typeName = data.getCate_name();
                            }
                        }
                        tvActivityModel.setText(typeName);
                        modelAddWindow.dismiss();

                    }
                });
        modelAddAdapter.setOnItemClickListener(this);
    }

    private void showSelectTime(final WhichTime whichTime) {
        String startTime = GetSystemTimeUtil.getStartTimeandHHMM();
        String endTime = GetSystemTimeUtil.getEndTimeandHHMM();
        if (whichTime == WhichTime.END_TIME) {
            if (!"活动开始前".equals(tvActivityEndTime.getText().toString())) {
                startTime = tvActivityStartTime.getText().toString().replace("/", "-");
            } else {
                if ("即日".equals(tvActivityStartTime.getText().toString())) {
                    startTime = GetSystemTimeUtil.getStartTimeandHHMM();
                } else {
                    startTime = tvActivityStartTime.getText().toString().replace("/", "-");
                }
                if (null != inviteTime) {
                    endTime = inviteTime;
                }
            }
        }
        new TimeSelector(this, new TimeSelector.ResultHandler() {
            @Override
            public void handle(String time) {
                changeTime = time;
                if (time != null) {
                    try {
                        L.e("TimeSelector:" + time);
                        if (whichTime == WhichTime.INVITE_TIME) {
                            tvTitleTime.setText(GetSystemTimeUtil.getNowTurnTime(time));
                            tvTitleDate.setText(GetSystemTimeUtil.getTurnWeekDate(time));
                            publishReqBean.setTime_start(String.valueOf(System.currentTimeMillis()));
                            publishReqBean.setTime_end(String.valueOf(GetSystemTimeUtil.dateToStamp(time)));
                            publishReqBean.setInvite_time(String.valueOf(GetSystemTimeUtil.dateToStamp(time)));
                            tvActivityStartTime.setText("即日");
                            tvActivityEndTime.setText("活动开始前");
                            stTimeSelect.setButtonName(time);
                            inviteTime = time;
                        } else if (whichTime == WhichTime.START_TIME) {
                            if (inviteTime != null) {
                                if (GetSystemTimeUtil.dateToStamp(time) >= GetSystemTimeUtil.dateToStamp(inviteTime)) {
                                    showToast("报名开始时间必须小于活动时间", R.mipmap.jinshi);
                                    return;
                                }
                            } else {
                                showToast("请先选择活动时间", R.mipmap.jinshi);
                                return;
                            }
                            if (!"".equals(publishReqBean.getTime_end())) {
                                if (Long.valueOf(publishReqBean.getTime_end()) <= GetSystemTimeUtil.dateToStamp(time)) {
                                    showToast("报名开始时间必须小于结束时间", R.mipmap.jinshi);
                                    return;
                                }
                            }
                            publishReqBean.setTime_start(String.valueOf(GetSystemTimeUtil.dateToStamp(time)));
                            tvActivityStartTime.setText(time.replace("-", "/"));
                        } else if (whichTime == WhichTime.END_TIME) {
                            if (!"".equals(publishReqBean.getTime_start())) {
                                if (Long.valueOf(publishReqBean.getTime_start()) >= GetSystemTimeUtil.dateToStamp(time)) {
                                    showToast("报名结束时间必须大于开始时间", R.mipmap.jinshi);
                                    return;
                                }
                            } else {
                                if (inviteTime == null) {
                                    showToast("请先选择活动时间", R.mipmap.jinshi);
                                    return;
                                }
                            }
                            publishReqBean.setTime_end(String.valueOf(GetSystemTimeUtil.dateToStamp(time)));
                            tvActivityEndTime.setText(time.replace("-", "/"));
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, startTime, endTime).show();

    }

    public void inviteUpdateFail(String error) {
        showToast(error, R.mipmap.jinshi);
        Router.pop(this);
    }

    public void inviteUpdateSucceed(String msg) {
        showToast(msg, R.mipmap.jinshi);
        Router.pop(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 12 && resultCode == RESULT_OK) {
            city = (City) data.getExtras().getSerializable("data");
            publishReqBean.setAddress(city.getAddress());
            publishReqBean.setRegion_id3(city.getCity_id());
            publishReqBean.setLandmark(city.getAreaName());
            publishReqBean.setLongitude(String.valueOf(city.getLongitude()));
            publishReqBean.setLatitude(String.valueOf(city.getLatitude()));
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    tvLocalSelect.setText(city.getAreaName());
                    tvLocalDetails.setText(city.getAddress());
                }
            };
            runOnUiThread(runnable);
        } else if (requestCode == 23 && resultCode == RESULT_OK) {
            String detail = data.getExtras().getString("detail");
            if (!TextUtil.isEmpty(detail)) {
                tvActivityDetail.setText(data.getExtras().getString("detail"));
                tvActivityDetailNumber.setText(Html.fromHtml("<font color='" + getResources().getColor(R.color.text_color) +
                        "'>" + detail.length() +
                        "</font>/500"), TextView.BufferType.SPANNABLE);
            }
        } else if (requestCode == 66 && resultCode == RESULT_OK) {
            picList = data.getExtras().getStringArrayList("pic");
            if (picList != null && picList.size() > 0) {
                picAdapter.setPicList(picList);
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (modelAddWindow != null && modelAddWindow.isShowing()) {
            modelAddWindow.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stTimeSelect.finshSeldct();
    }

    @Override
    protected void onResume() {
        super.onResume();
        stTimeSelect.setchecknendable();
        stTimeSelect.setSelectStatus();

    }

    @Override
    protected void setScreen() {
        super.setScreen();
        hideSoftKeyboard();
    }


    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b) {
            llNearby.setVisibility(View.VISIBLE);
            tvPointTip.setVisibility(View.VISIBLE);
            Handler mHandler = new Handler();
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    svMain.fullScroll(ScrollView.FOCUS_DOWN);
                }
            });
            if (!typeHasGet) {
                getP().getTypeList();
            }
        } else {
            llNearby.setVisibility(View.GONE);
            tvPointTip.setVisibility(View.GONE);
        }
    }

    /**
     * 获取活动type成功
     *
     * @param data
     */
    public void getTypeListSucceed(List<GetTypeListBean.DataBean> data) {
        typeList = data;
        typeHasGet = true;
        for (int i = 0; i < typeList.size(); i++) {
            typeList.get(i).setSelect(false);
        }
        modelAddAdapter.setTypeList(data);

    }

    public void getTypeListFail(String msg) {

    }

    @Override
    public void onClick(List<GetTypeListBean.DataBean> typeList, int position) {
        for (GetTypeListBean.DataBean bean : typeList) {
            bean.setSelect(false);
        }
        typeList.get(position).setSelect(true);
        publishReqBean.setType_id(typeList.get(position).getId());
        modelAddAdapter.setTypeList(typeList);
    }

    @Override
    public void onLongClick(List<GetTypeListBean.DataBean> typeList, int position) {

    }

    @Override
    public void onClick(int position) {
        if (position == picList.size()) {
            Router.newIntent((Activity) App.getInstance())
                    .requestCode(66)
                    .putBoolean(UserLogoActivity.EXTRA_SHOW_CAMERA, true)
                    .putInt(UserLogoActivity.EXTRA_SELECT_COUNT, 3)
                    .putInt(UserLogoActivity.EXTRA_SELECT_MODE, UserLogoActivity.MODE_MULTI)
                    .putStringArrayList(UserLogoActivity.EXTRA_DEFAULT_SELECTED_LIST, picList)
                    .putBoolean(UserLogoActivity.EXTRA_SHOW_CAMERA, false)
                    .to(UserLogoActivity.class)
                    .launch();
        }
    }

    @Override
    public void onDelClick(int position) {
        if (picList.size() > position) {
            picList.remove(position);
            picAdapter.setPicList(picList);
        }
    }


    @Override
    public boolean useEventBus() {
        return true;
    }

    @Override
    public void bindEvent() {
        super.bindEvent();
        BusProvider
                .getBus()
                .toObservable(CoverPicEvent.class)
                .compose(this.<CoverPicEvent>bindToLifecycle())
                .subscribe(new Action1<CoverPicEvent>() {
                    @Override
                    public void call(CoverPicEvent coverPicEvent) {
                        coverPicPath = coverPicEvent.getCoverPicPath();
                        publishReqBean.setCoverBitmapPath(coverPicPath);
                        Picasso.with(App.getInstance())
                                .load(new File(coverPicPath))
                                .resize(Kits.Dimens.dpToPxInt(EditorInvitedActivity.this, 200), Kits.Dimens.dpToPxInt(EditorInvitedActivity.this, 120))
                                .error(R.mipmap.activity_loading)
                                .placeholder(R.mipmap.activity_loading)
                                .into(ivActivityCover);
                    }
                });

    }
}
